package com.freak.neteasecloudmusic.modules.find.recommend.songlist.detail;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.freak.circularbeadimageview.CircularBeadClipPathImageView;
import com.freak.httphelper.log.LogUtil;
import com.freak.neteasecloudmusic.R;
import com.freak.neteasecloudmusic.base.BaseAbstractMvpWithPlayerActivity;
import com.freak.neteasecloudmusic.base.IActivityStatusBar;
import com.freak.neteasecloudmusic.modules.find.recommend.entity.SongListDetailEntity;
import com.freak.neteasecloudmusic.modules.find.recommend.entity.SongUrlEntity;
import com.freak.neteasecloudmusic.modules.find.recommend.songlist.detail.adapter.SongListDetailAdapter;
import com.freak.neteasecloudmusic.modules.find.recommend.songlist.detail.comment.CommentActivity;
import com.freak.neteasecloudmusic.modules.find.recommend.songlist.detail.comment.entity.CommentHeadEntity;
import com.freak.neteasecloudmusic.modules.find.recommend.songlist.fragment.entity.PlaylistsEntity;
import com.freak.neteasecloudmusic.utils.StringUtils;
import com.freak.neteasecloudmusic.view.custom.toolbar.SimpleToolbar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * 歌单详情
 *
 * @author Administrator
 * @date 2019/3/20
 */

public class DetailActivity extends BaseAbstractMvpWithPlayerActivity<DetailPresenter> implements DetailContract.View, View.OnClickListener, IActivityStatusBar {

    @BindView(R.id.textViewSongListDetailContext)
    TextView textViewSongListDetailContext;
    @BindView(R.id.textViewSongListDetailListenerCount)
    TextView textViewSongListDetailListenerCount;
    @BindView(R.id.textViewSongListDetailAuthor)
    TextView textViewSongListDetailAuthor;
    @BindView(R.id.textViewSongListDetailDetail)
    TextView textViewSongListDetailDetail;
    @BindView(R.id.viewArrow)
    View viewArrow;
    @BindView(R.id.imageViewSongListDetailCommentCount)
    ImageView imageViewSongListDetailCommentCount;
    @BindView(R.id.textViewSongListDetailCommentCount)
    TextView textViewSongListDetailCommentCount;
    @BindView(R.id.constraintLayoutSongListDetailCommentCount)
    ConstraintLayout constraintLayoutSongListDetailCommentCount;
    @BindView(R.id.imageViewSongListDetailShare)
    ImageView imageViewSongListDetailShare;
    @BindView(R.id.textViewSongListDetailShare)
    TextView textViewSongListDetailShare;
    @BindView(R.id.constraintLayoutSongListDetailShare)
    ConstraintLayout constraintLayoutSongListDetailShare;
    @BindView(R.id.imageViewSongListDetailDown)
    ImageView imageViewSongListDetailDown;
    @BindView(R.id.textViewSongListDetailDown)
    TextView textViewSongListDetailDown;
    @BindView(R.id.constraintLayoutSongListDetailDown)
    ConstraintLayout constraintLayoutSongListDetailDown;
    @BindView(R.id.imageViewSongListDetailMoreSelect)
    ImageView imageViewSongListDetailMoreSelect;
    @BindView(R.id.textViewSongListDetailMoreSelect)
    TextView textViewSongListDetailMoreSelect;
    @BindView(R.id.constraintLayoutSongListDetailMoreSelect)
    ConstraintLayout constraintLayoutSongListDetailMoreSelect;
    @BindView(R.id.imageViewSongListDetailBg)
    CircularBeadClipPathImageView imageViewSongListDetailBg;
    @BindView(R.id.imageViewSongListDetailPhotoBg)
    CircularBeadClipPathImageView imageViewSongListDetailPhotoBg;
    @BindView(R.id.imageViewSongListDetailStartAll)
    ImageView imageViewSongListDetailStartAll;
    @BindView(R.id.textViewSongListDetailAllSongCount)
    TextView textViewSongListDetailAllSongCount;
    @BindView(R.id.imageViewSongListDetailAdd)
    ImageView imageViewSongListDetailAdd;
    @BindView(R.id.text_view_song_list_detail_collect)
    TextView textViewSongListDetailCollect;
    @BindView(R.id.recyclerViewSongLiseDetail)
    RecyclerView recyclerViewSongLiseDetail;
    @BindView(R.id.coordinatorLayoutSongListDetail)
    CoordinatorLayout coordinatorLayoutSongListDetail;
    @BindView(R.id.simple_toolbar_song_list_detail)
    SimpleToolbar simpleToolbarSongListDetail;
    @BindView(R.id.constraintLayoutCollectCount)
    ConstraintLayout constraintLayoutCollectCount;
    private String id;
    private Bundle bundle;
    private SongListDetailAdapter mSongListDetailAdapter;
    private List<PlaylistsEntity.TracksBean> mList;
    private CommentHeadEntity mCommentHeadEntity;


    @Override
    public void onClick(View v) {

    }

    @Override
    public void showToast(String toast) {

    }

    @Override
    protected int getLayout() {
        return R.layout.activity_song_list_detail;
    }

    @Override
    protected void initEventAndData() {
        mPresenter.getSongListDetail(id);
    }

    @Override
    protected void onCreateLoadData() {

    }

    @Override
    protected void onDestroyRelease() {

    }

    @Override
    protected void onResumeLoadData() {

    }

    @Override
    protected void initView() {
        simpleToolbarSongListDetail.setLeftIconClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        }).setRightIconClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        bundle = getIntent().getExtras();
        if (bundle == null) {
            finish();
            return;
        }
        mCommentHeadEntity = new CommentHeadEntity();
        id = bundle.getString("id");
        mList = new ArrayList<>();
        mCommentHeadEntity.setId(id);
        recyclerViewSongLiseDetail.setLayoutManager(new LinearLayoutManager(this));
        mSongListDetailAdapter = new SongListDetailAdapter(R.layout.item_song_list_detail, mList);
        mSongListDetailAdapter.bindToRecyclerView(recyclerViewSongLiseDetail);
        mSongListDetailAdapter.setEmptyView(R.layout.view_no_data_common, recyclerViewSongLiseDetail);
        recyclerViewSongLiseDetail.setAdapter(mSongListDetailAdapter);

        mSongListDetailAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                songInfo = mList.get(position);
//                mPresenter.loadSongUrl(songInfo.getId());
            }
        });
        mSongListDetailAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

            }
        });
    }


    @Override
    protected DetailPresenter createPresenter() {
        return new DetailPresenter();
    }

    @Override
    public void getSongListDetailSuccess(SongListDetailEntity songListDetailEntity) {
        mList.clear();
        if (songListDetailEntity != null) {
            LogUtil.e("songListDetailEntity " + songListDetailEntity.toString());
            Glide.with(this).load(songListDetailEntity.getPlaylist().getCoverImgUrl()).thumbnail(0.f).into(imageViewSongListDetailBg);
            Glide.with(this).load(songListDetailEntity.getPlaylist().getCreator().getAvatarUrl()).thumbnail(0.f).into(imageViewSongListDetailPhotoBg);
            textViewSongListDetailAuthor.setText(songListDetailEntity.getPlaylist().getCreator().getNickname());
            textViewSongListDetailDetail.setText(songListDetailEntity.getPlaylist().getCreator().getSignature());
            textViewSongListDetailContext.setText(songListDetailEntity.getPlaylist().getName());
            mCommentHeadEntity.setAuthor(songListDetailEntity.getPlaylist().getCreator().getNickname());
            mCommentHeadEntity.setContext(songListDetailEntity.getPlaylist().getCreator().getNickname());
            mCommentHeadEntity.setUrl(songListDetailEntity.getPlaylist().getCoverImgUrl());
            textViewSongListDetailListenerCount.setText(StringUtils.toOneHundredThousand(Long.parseLong(songListDetailEntity.getPlaylist().getPlayCount())));
            textViewSongListDetailCommentCount.setText(StringUtils.compareTo(songListDetailEntity.getPlaylist().getCommentCount(), "0") == 0 ? "评论" : songListDetailEntity.getPlaylist().getCommentCount());
            textViewSongListDetailShare.setText(StringUtils.compareTo(songListDetailEntity.getPlaylist().getShareCount(), "0") == 0 ? "评论" : songListDetailEntity.getPlaylist().getShareCount());
            textViewSongListDetailDetail.setText(songListDetailEntity.getPlaylist().getDescription());
            textViewSongListDetailCollect.setText(String.format(getResources().getString(R.string.all_song_collect), StringUtils.toOneHundredThousand(Long.parseLong(songListDetailEntity.getPlaylist().getSubscribedCount()))));
            textViewSongListDetailAllSongCount.setText(String.format(getResources().getString(R.string.all_song_count), songListDetailEntity.getPlaylist().getTrackCount()));
            constraintLayoutCollectCount.setVisibility(StringUtils.compareTo(songListDetailEntity.getPlaylist().getSubscribedCount(),"0")==0?View.GONE:View.VISIBLE);

            if (songListDetailEntity.getPlaylist().getTracks() != null && songListDetailEntity.getPlaylist().getTracks().size() > 0) {
                mList.addAll(songListDetailEntity.getPlaylist().getTracks());
                mSongListDetailAdapter.notifyDataSetChanged();
            }
        }

    }

    @Override
    public void loadSongUrlSuccess(SongUrlEntity songUrlEntity) {
        LogUtil.e("播放歌曲信息 --》" + songUrlEntity.toString());
//        AudioInfo audioInfo = AudioInfoTransitionUtil.audioInfoTransition(songUrlEntity.getData().get(0), songInfo);
//        AudioInfo audioInfo = new AudioInfo();
//        audioInfo.setDownloadUrl(songUrlEntity.getData().get(0).getUrl());
//        audioInfo.setType(AudioInfo.TYPE_LOCAL);
//        audioInfo.setHash(songUrlEntity.getData().get(0).getMd5());
//        AudioPlayerManager.getInstance(this).playSongAndAdd(audioInfo);
    }

    @Override
    public int getStatusBarColor() {
        return getResources().getColor(R.color.color_FF3F3334);
    }

    @Override
    public int getDrawableStatusBar() {
        return 0;
    }


    @OnClick({R.id.constraintLayoutSongListDetailCommentCount, R.id.constraintLayoutSongListDetailShare, R.id.constraintLayoutSongListDetailDown, R.id.constraintLayoutSongListDetailMoreSelect})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            //评论
            case R.id.constraintLayoutSongListDetailCommentCount:
                Bundle bundle = new Bundle();
                bundle.putParcelable("mCommentHeadEntity", mCommentHeadEntity);
                gotoActivity(CommentActivity.class, false, bundle);
                break;
            case R.id.constraintLayoutSongListDetailShare:
                break;
            case R.id.constraintLayoutSongListDetailDown:
                break;
            case R.id.constraintLayoutSongListDetailMoreSelect:
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
